package com.test.valuestack;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.util.ValueStack;
import com.test.tagtest.Person;
import lombok.Getter;
import lombok.Setter;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author lzy
 * @version v1.0
 * Create DateTime: 2018/9/9 10:13
 */
@Setter
@Getter
public class Product implements RequestAware, SessionAware {
    private Integer productId;
    private String productName;
    private String productDesc;
    private Double productPrice;

    private List<Person> persons = new ArrayList<>();
    private Map<String, Object> sessionMap;
    private Map<String, Object> requestMap;

    public String testTag() {
        this.productId = 5001;
        this.productName = "GPU";
        this.productDesc = "AMD";
        this.productPrice = 1234.45;

        persons.add(new Person("xiaowang", 52));
        persons.add(new Person("xiaoli", 56));
        persons.add(new Person("xiaohong", 52));
        persons.add(new Person("xiaofang", 95));
        persons.add(new Person("xiaoxie", 25));
        persons.add(new Person("xiaofeng", 19));


        return "success";
    }

    public String save() {
        System.out.println("save" + this);

        //1.获取值栈
        ValueStack valueStack = ActionContext.getContext().getValueStack();

        //2.创建Test对象，并为其属性赋值
        Test object = new Test();
        object.setProductName("abc");
        object.setProductDesc("aabbcc");

        //3.把Test对象压入到zh值栈的栈顶
        valueStack.push(object);

        sessionMap.put("product", this);
        requestMap.put("test", object);

        // 测试异常处理
//        int i = 1/0;

        return "success";
    }

    @Override
    public void setRequest(Map<String, Object> request) {
        this.requestMap = request;
    }

    @Override
    public void setSession(Map<String, Object> session) {
        this.sessionMap = session;
    }
}
